<template>
    <div>
        <router-link class="link" :to="'/user/'+user._id">
            <img :src="user.avatar" width="45" height="45" class="circle">
            <div class="info-box">
                <router-link class="username" :to="'/user/'+user._id">{{user.nickname}}</router-link>
                <div class="detail">前端工程师 @ 闭关中</div>
            </div>
            <button v-if="user._id!=loginUser.id" class="follow-btn" @click.prevent="onclick_follow"
                    :class="{isFollowed:isFollowed}">
                {{isFollowed?"已关注":"关注"}}
            </button>
        </router-link>
    </div>
</template>

<script>
    export default {
        name: "UserFollowItem",
        props: ['user'],
        computed: {
            loginUser() {
                return this.$store.state.user
            },
            isFollowed() {
                return this.loginUser.following.includes(this.user._id)
            }
        },
        created() {
            console.log(this.user)
        },
        methods: {
            onclick_follow() {
                this.$emit("onclick_follow", this.user._id, this.isFollowed)
            }
        }
    }
</script>

<style scoped lang="less">
    .link {
        display: flex;
        align-items: center;
        padding: 6px 28px;
        min-height: 96px;
        border-bottom: 1px solid rgba(230, 230, 231, .5);

        .circle {
            margin-right: 20px;
        }

        .info-box {
            flex: 1 1 auto;

            .username {
                font-size: 16px;
                font-weight: 600;
                color: #2e3135;
            }

            .detail {
                margin-top: 7px;
                font-size: 12px;
                font-weight: 500;
                color: #b9c0c8;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
        }

        .follow-btn {
            flex: 0 0 auto;
            margin: 0 0 0 12px;
            padding: 0;
            width: 88px;
            height: 28px;
            font-size: 12px;
            color: #92c452;
            background-color: #fff;
            border: 1px solid #92c452;
            border-radius: 2px;
        }

        .isFollowed {
            color: #fff;
            background-color: #92c452;
        }
    }


</style>
